home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / console / svgatext.3 / svgatext / SVGATextMode-1.3 / ClockProg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-05  |  5.8 KB  |  221 lines

  1. /*  SVGATextMode -- An SVGA textmode manipulation/enhancement tool
  2.  *
  3.  *  Copyright (C) 1995,1996  Koen Gadeyne
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20.  
  21. /***
  22.  *** ClockProg: Works like SVGATextMode, but programs only pixel and memory clock.
  23.  ***            Could maybe be used as an XFree86 ClockProg, because it enables
  24.  ***            the low clocks needed for 320x200 modes in X, which X seems to
  25.  ***            deny, despite the "divide-by-2" mode that all VGA's have.
  26.  ***/
  27.  
  28. #include "misc.h"
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <math.h>
  35.  
  36. #define CHIPSETREC 1  /* included chipset definition struct */
  37. #include "chipset.h"
  38. #include "vga_prg.h" 
  39. #include "setclock.h"
  40. #include "file_ops.h"
  41. #include "string_ops.h"
  42. #include "messages.h"
  43. #include "chipset.h"
  44. #include "validate.h"
  45. #include "run_extprog.h"
  46.  
  47. /*
  48.  * this adds all the global variables that hold the data from the config file.
  49.  */
  50. #include "cfg_data.h"
  51.  
  52. /*
  53.  * other global variables
  54.  */
  55.  
  56. char *CommandName;
  57. char *ConfigFile=CLOCK_CONF_FILE;
  58. bool debug_messages=FALSE;
  59.  
  60. /*
  61.  * yacc parser functions
  62.  */
  63.  
  64. extern int yyparse(void);
  65. extern FILE *yyin;
  66. #define param_file yyin
  67.  
  68. #undef USE_INDEX
  69.  
  70. void usage()
  71. {
  72.      PMESSAGE(("version %s. (c) 1995,1996 Koen Gadeyne.\n"\
  73.      "  Usage: %s [options] <Frequency> [<Index>]\n"\
  74.      "  Options: -n  Don't program VGA hardware\n"\
  75.      "               (don't do actual pixel clock programming)\n"\
  76.      "           -d  print debugging information\n"\
  77.      "           -h  print usage information\n"\
  78.      "           -m <Frequency>\n"\
  79.      "               Program memory clock with given frequency\n"\
  80.      "               (only works on some clockchips)\n"\
  81.      "           -t <ConfigFile>\n"\
  82.      "               Use <ConfigFile> instead of the default.\n"\
  83.      "               (default = %s if that exists, or %s otherwise)\n"\
  84.      "  <Frequency>: A pixel clock frequency in MHz\n"\
  85.      "  <Index>: The index in the list of available pixel clocks\n"\
  86.      "           Allowed for compatibility with XFree86, but ignored.\n"\
  87.      "           clock specification in config file is used, not this index\n",\
  88.      VERSION, CommandName, CLOCK_CONF_FILE, CONFIGFILE));
  89. }
  90.  
  91. /****************************************************************************************************************************/
  92.  
  93. int main (int argc, char* argv[])
  94. {
  95.   bool program_hardware=TRUE;
  96.   int freq, mclk=MCLK_NOT_DEFINED;
  97.   int c;
  98. #ifdef USE_INDEX
  99.   int index;
  100. #endif
  101.     
  102.  /*
  103.   * command-line argument parsing
  104.   */
  105.  
  106.   CommandName = argv[0];
  107.  
  108.   while ((c = getopt (argc, argv, "ndhm:t:")) != EOF)
  109.     switch (c)
  110.     {
  111.       case 'n': program_hardware=FALSE;
  112.                 break;
  113.       case 'd': debug_messages=TRUE;
  114.                 break;
  115.       case 'h': usage();
  116.                 exit(0);
  117.                 break;
  118.       case 't': ConfigFile=safe_strdup(optarg);
  119.                 break;
  120.       case 'm': mclk=getfloat(optarg, "memory clock frequency", 0, 70)*1000;
  121.                 PDEBUG(("MClk on command line = %d khz\n", mclk));
  122.                 break;
  123.       case '?': usage();
  124.                 PERROR(("Bad option '-%c'\n",(char)optopt));
  125.                 exit(-1);
  126.                 break;
  127.       default: PERROR(("getopt returned unknown token '%c'.\n",c));
  128.     }
  129.    
  130.   PVERSION;
  131.  
  132. #ifdef USE_INDEX
  133.   if (argc<optind+2)
  134. #else
  135.   if (argc<optind+1)
  136. #endif
  137.   {
  138.     usage();
  139.     PERROR(("Not enough parameters on command line.\n"));
  140.   }
  141.  
  142.   /* first argument: clock freq in MHz */
  143.   freq = getfloat(argv[optind], "clock frequency", 1, 500)*1000;
  144.   
  145. #ifdef USE_INDEX
  146.   /* second argument: clock index */
  147.   optind++;
  148.   index = getint(argv[optind], "clock index", 0, 127);
  149. #endif
  150.  
  151.  /*
  152.   * check if default config file exists. If not, use TextConfig.
  153.   */
  154.   
  155.   if ((param_file=fopen(ConfigFile,"r"))==NULL)
  156.   {
  157.     PDEBUG(("`%s' not found, using `%s'\n", ConfigFile, CONFIGFILE));
  158.     ConfigFile=safe_strdup(CONFIGFILE);
  159.   }
  160.   else
  161.   {
  162.     fclose(param_file);
  163.     PDEBUG(("Using `%s' as config file\n", ConfigFile));
  164.   }
  165.  
  166.  /*
  167.   * open parameter file, parse the sucker, close up again.
  168.   */
  169.   param_file = open_param_file(ConfigFile);
  170.   PDEBUG(("Parsing Config file...\n"));
  171.   while (!feof(yyin)) { yyparse(); }
  172.   fclose(param_file);
  173.  
  174.  /*
  175.   * Do some sanity checks
  176.   */
  177.   
  178.   sanitize_cfgfile_data();
  179.  
  180.   /* 3rd argument: memory clock */
  181.   optind++;
  182.   if (argc > optind)
  183.   {
  184.     if (clock_data.mclk != MCLK_NOT_DEFINED)
  185.       PDEBUG(("Overriding MClk in config file by value on command line: %d kHz\n", mclk));
  186.     clock_data.mclk = mclk;
  187.   }
  188.  
  189.  /*
  190.   * See if this setup can generate that clock, and abort of not.
  191.   */
  192.   
  193.   validate_clock(freq, TRUE);
  194.  
  195.  /*
  196.   * finally do it... maybe
  197.   */
  198.   
  199.   if (program_hardware)
  200.   {
  201.     get_VGA_io_perm(chipset);
  202.  
  203.     /* sync disks if requested. Is there any way to do this and be SURE _all_ data has been flushed? */
  204.     if (OFLG_ISSET(OPT_SYNC))
  205.     {
  206.       PMESSAGE(("Syncing disks...\n"));
  207.       sync(); sleep(2); /* same as in "reboot" command. Hopefully enough */
  208.     }
  209.  
  210.     unlock(chipset);
  211.  
  212.     PMESSAGE(("Setting Pixel Clock to %1.2f MHz.\n", freq/1000.0));    
  213.     do_clock(chipset, freq);
  214.  
  215.     special(chipset); /* change chipset-specific things, if needed */
  216.  
  217.   }
  218.   return(0);
  219. }
  220.  
  221.